package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.astrob.view.BaseViewHolder;
import com.gpx2shp.JniUtils;
import com.nhc.database.dbmodel.Route;
import com.nhc.database.dbmodel.WayPoint;
import com.nhc.database.dbmodel.WayTrail;
import com.nhc.database.manager.DBDataCenter;
import com.nhc.database.manager.IECenter;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.AlertDialog.OnDialogButtonClickListener;
import zuo.biao.library.ui.ItemDialog;
import zuo.biao.library.ui.PageScroller;
import zuo.biao.library.ui.TopMenuWindow;

public class DaochuActivity extends BaseActivity implements OnDialogButtonClickListener, View.OnClickListener, ItemDialog.OnDialogItemClickListener {
    private final String TAG = "DaochuActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, DaochuActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daochu_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_all;
    private TextView tv_daochu;
    private TextView iv_stydl_0;
    private TextView iv_stydl_1;
    private TextView iv_stydl_2;

    private TextView iv_check0;
    private TextView iv_check1;
    private TextView iv_check2;

    private Button bt_selectall;
    private Button bt_share;
    private Button bt_daochu;


    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_all = (ListView) findViewById(R.id.lv_all);

        tv_daochu = (TextView) findViewById(R.id.tv_daochu);
        tv_daochu.setTypeface(AppContext.getInstances().getIconfont());

        bt_daochu = (Button) findViewById(R.id.bt_daochu);
        bt_selectall = (Button) findViewById(R.id.bt_selectall);
        bt_share = (Button) findViewById(R.id.bt_share);

        iv_stydl_0 = (TextView) findViewById(R.id.iv_stydl_0);
        iv_stydl_1 = (TextView) findViewById(R.id.iv_stydl_1);
        iv_stydl_2 = (TextView) findViewById(R.id.iv_stydl_2);

        iv_check0 = (TextView) findViewById(R.id.iv_check0);
        iv_check1 = (TextView) findViewById(R.id.iv_check1);
        iv_check2 = (TextView) findViewById(R.id.iv_check2);

        iv_check0.setTypeface(AppContext.getInstances().getIconfont());
        iv_check1.setTypeface(AppContext.getInstances().getIconfont());
        iv_check2.setTypeface(AppContext.getInstances().getIconfont());

        iv_check0.setText(R.string.fonticon_xz);
        iv_check1.setText(R.string.fonticon_xz);
        iv_check2.setText(R.string.fonticon_xz);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     */
    private void setList() {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_all.setAdapter(adapter);
        }
        if (_mode == 0) {
            adapter.refresh(naviptlist);
        } else if (_mode == 1) {
            adapter.refresh(navilinelist);
        } else {
            adapter.refresh(navitracklist);
        }

        if (naviptlist.size() == 0) {
            iv_check0.setText(R.string.fonticon_wxz);
            iv_check0.setEnabled(false);
        }

        if (navilinelist.size() == 0) {
            iv_check1.setText(R.string.fonticon_wxz);
            iv_check1.setEnabled(false);
        }

        if (navitracklist.size() == 0) {
            iv_check2.setText(R.string.fonticon_wxz);
            iv_check2.setEnabled(false);
        }

    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private int _mode = 0;//0-航点 1-航线 2-航迹
    private List<Entry<Integer, String>> naviptlist;
    private ArrayList<WayPoint> mNaviptList = new ArrayList<WayPoint>();
    private HashMap<Long, Boolean> hashMap;

    private List<Entry<Integer, String>> navilinelist;
    private ArrayList<Route> mNaviLineList = new ArrayList<Route>();
    private HashMap<Long, Boolean> hashMap1;

    private List<Entry<Integer, String>> navitracklist;
    private ArrayList<WayTrail> mNaviTrackList = new ArrayList<WayTrail>();
    private HashMap<Long, Boolean> hashMap2;

    //排序方式
    int _order = 0;//0-id 时间 1-name

    private void resetAlldata() {
        mNaviptList = DBDataCenter.getInstance().getWaypointListByOrder(_order);
        mNaviLineList = DBDataCenter.getInstance().getRouteList(_order);

        ArrayList<WayTrail> naviTrackList = DBDataCenter.getInstance().getWaytrailListByOrder(_order);
        //航迹列表，不显示当前的航迹
        //历史航迹的行程距离应该都>0
        mNaviTrackList.clear();
        Long id = DBDataCenter.getInstance().getThisWayTrailID();
        for (WayTrail wt : naviTrackList) {
            if (id == wt.getId()) {
                continue;
            }
            mNaviTrackList.add(wt);
        }

        //-----
        naviptlist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mNaviptList.size(); i++) {
            naviptlist.add(new Entry<Integer, String>(i, ""));
        }

        navilinelist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mNaviLineList.size(); i++) {
            navilinelist.add(new Entry<Integer, String>(i, ""));
        }

        navitracklist = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < mNaviTrackList.size(); i++) {
            navitracklist.add(new Entry<Integer, String>(i, ""));
        }

        //-----
        boolean isSelected = true;//初始化全选中
        if (hashMap == null) {
            hashMap = new HashMap<Long, Boolean>();
            for (int i = 0; i < mNaviptList.size(); i++) {
                hashMap.put(mNaviptList.get(i).getId(), isSelected);
            }
        }

        if (hashMap1 == null) {
            hashMap1 = new HashMap<Long, Boolean>();
            for (int i = 0; i < mNaviLineList.size(); i++) {
                hashMap1.put(mNaviLineList.get(i).getId(), isSelected);
            }
        }

        if (hashMap2 == null) {
            hashMap2 = new HashMap<Long, Boolean>();
            for (int i = 0; i < mNaviTrackList.size(); i++) {
                hashMap2.put(mNaviTrackList.get(i).getId(), isSelected);
            }
        }


    }

    private void onClickItem(int i) {
        if (_mode == 0) {
            long id = mNaviptList.get(i).getId();
            boolean isSelected = hashMap.get(id);
            hashMap.put(id, !isSelected);
        } else if (_mode == 1) {
            long id = mNaviLineList.get(i).getId();
            boolean isSelected = hashMap1.get(id);
            hashMap1.put(id, !isSelected);
        } else {
            long id = mNaviTrackList.get(i).getId();
            boolean isSelected = hashMap2.get(id);
            hashMap2.put(id, !isSelected);
        }

        if (hashMap.containsValue(false)) {
            iv_check0.setText(R.string.fonticon_wxz);
        } else {
            iv_check0.setText(R.string.fonticon_xz);
        }

        if (hashMap1.containsValue(false)) {
            iv_check1.setText(R.string.fonticon_wxz);
        } else {
            iv_check1.setText(R.string.fonticon_xz);
        }

        if (hashMap2.containsValue(false)) {
            iv_check2.setText(R.string.fonticon_wxz);
        } else {
            iv_check2.setText(R.string.fonticon_xz);
        }

        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    //示例代码>>>>>>>>>

    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                resetAlldata();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList();
                        bt_selectall.setText(getString(R.string.selectallno));
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<

        tv_daochu.setOnClickListener(this);
        bt_daochu.setOnClickListener(this);

        findViewById(R.id.ll_ctrl_0).setOnClickListener(this);
        findViewById(R.id.ll_ctrl_1).setOnClickListener(this);
        findViewById(R.id.ll_ctrl_2).setOnClickListener(this);
        iv_check0.setOnClickListener(this);
        iv_check1.setOnClickListener(this);
        iv_check2.setOnClickListener(this);

        lv_all.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onClickItem(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_all).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void onForwardClick(View v) {
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu_daochu)), REQUEST_TO_TOP_MENU, false);
    }

    public void selectAll(boolean isSelected, int mode) {
        if (mode == 0) {
            for (int i = 0; i < mNaviptList.size(); i++) {
                hashMap.put(mNaviptList.get(i).getId(), isSelected);
            }
            if (hashMap.containsValue(false)) {
                iv_check0.setText(R.string.fonticon_wxz);
            } else {
                iv_check0.setText(R.string.fonticon_xz);
            }
        } else if (mode == 1) {
            for (int i = 0; i < mNaviLineList.size(); i++) {
                hashMap1.put(mNaviLineList.get(i).getId(), isSelected);
            }
            if (hashMap1.containsValue(false)) {
                iv_check1.setText(R.string.fonticon_wxz);
            } else {
                iv_check1.setText(R.string.fonticon_xz);
            }
        } else {
            for (int i = 0; i < mNaviTrackList.size(); i++) {
                hashMap2.put(mNaviTrackList.get(i).getId(), isSelected);
            }
            if (hashMap2.containsValue(false)) {
                iv_check2.setText(R.string.fonticon_wxz);
            } else {
                iv_check2.setText(R.string.fonticon_xz);
            }
        }

        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    boolean _isSelectedAll = true;
    public void onSelectAll(View v) {
        if (_isSelectedAll) {
            selectAll(false, 0);
            selectAll(false, 1);
            selectAll(false, 2);
            _isSelectedAll = false;
            bt_selectall.setText(getString(R.string.selectall));
        } else {
//            selectAll(true, _mode);
            selectAll(true, 0);
            selectAll(true, 1);
            selectAll(true, 2);
            _isSelectedAll = true;
            bt_selectall.setText(getString(R.string.selectallno));
        }
    }

    boolean _needshare = false;

    public void onShare(View v) {
        _needshare = true;
        onDaochu();
    }


    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_daochu:
            case R.id.bt_daochu:
                _needshare = false;
                onDaochu();
                break;
            case R.id.ll_ctrl_0:
                setMode(0);
                break;
            case R.id.ll_ctrl_1:
                setMode(1);
                break;
            case R.id.ll_ctrl_2:
                setMode(2);
                break;
            case R.id.iv_check0:
                onCheck(0);
                break;
            case R.id.iv_check1:
                onCheck(1);
                break;
            case R.id.iv_check2:
                onCheck(2);
                break;
            default:
                break;
        }
    }

    private void setMode(int pos) {
        if (pos == _mode) {
            return;
        }
        _mode = pos;
        iv_stydl_0.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
        iv_stydl_1.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
        iv_stydl_2.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));

        iv_check0.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
        iv_check1.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
        iv_check2.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_normal));
        switch (_mode) {
            default:
            case 0:
                iv_stydl_0.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                iv_check0.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                break;
            case 1:
                iv_stydl_1.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                iv_check1.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                break;
            case 2:
                iv_stydl_2.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                iv_check2.setTextColor(context.getResources().getColor(R.color.nhc_maintab_txt_selected));
                break;
        }

        setList();
    }

    private void onCheck(int pos) {
        if (pos == 0) {
            if (hashMap.containsValue(false)) {
                selectAll(true, 0);
            } else {
                selectAll(false, 0);
            }
        } else if (pos == 1) {
            if (hashMap1.containsValue(false)) {
                selectAll(true, 1);
            } else {
                selectAll(false, 1);
            }
        } else {
            if (hashMap2.containsValue(false)) {
                selectAll(true, 2);
            } else {
                selectAll(false, 2);
            }
        }

    }

    private final String[] TOPBAR_NAMES = {"GPX", "KML", "SHP"};
    private final String[] TOPBAR_NAMES_SHARE = {"GPX", "KML"};

    public void onDaochu() {
        if (_needshare) {
            new ItemDialog(context, TOPBAR_NAMES_SHARE, getString(R.string.share_gs), DIALOG_SET_TOPBAR, this).show();
        } else {
            new ItemDialog(context, TOPBAR_NAMES, getString(R.string.daochu_item), DIALOG_SET_TOPBAR, this).show();
        }
    }

    //pos   0-GPX   1-KML  2-SHP
    private void onDoNext(int pos) {
        showProgressDialog(R.string.loading);
        final int style = pos;
        runThread(TAG + "daochuData", new Runnable() {
            @Override
            public void run() {
                doDaochu(style);
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        if (style == 2) {
                            finishSHPDaochu();
                        } else {
                            finishDaochu();
                        }

                    }
                });
            }
        });
    }

    private void finishSHPDaochu() {
        showShortToast("SHP：" + _savePath);
    }

    String _daochuInfo = "";

    private void doDaochu(int style) {
        ArrayList<WayPoint> naviptList = new ArrayList<WayPoint>();
        for (int i = 0; i < mNaviptList.size(); i++) {
            if (hashMap.get(mNaviptList.get(i).getId())) {
                naviptList.add(mNaviptList.get(i));
            }
        }

        ArrayList<Route> naviLineList = new ArrayList<Route>();
        for (int i = 0; i < mNaviLineList.size(); i++) {
            if (hashMap1.get(mNaviLineList.get(i).getId())) {
                Route rt = mNaviLineList.get(i);
                rt.waypointList = DBDataCenter.getInstance().getWaypointListByRoute(rt);
                naviLineList.add(rt);
            }
        }

        ArrayList<WayTrail> naviTrackList = new ArrayList<WayTrail>();
        for (int i = 0; i < mNaviTrackList.size(); i++) {
            if (hashMap2.get(mNaviTrackList.get(i).getId())) {
                WayTrail wt = mNaviTrackList.get(i);
                wt.trackPointList = DBDataCenter.getInstance().getTrackPointByWayTrail(wt, null);
                naviTrackList.add(mNaviTrackList.get(i));
            }
        }

        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyyMMdd_HHmmss");
        Date curDate = new Date(System.currentTimeMillis());
        String sufix = formatter.format(curDate);
        if (style == 0 || style == 2) {
            IECenter.getInstance().export2GPX(sufix, naviptList, naviLineList, naviTrackList);
            _daochuInfo = String.format(getString(R.string.daochu_format), naviptList.size(), naviLineList.size(),
                    naviTrackList.size(), IECenter.EXDIR_GPX + sufix + ".gpx");

            _savePath = IECenter.EXDIR_GPX + sufix + ".gpx";
        } else {
            IECenter.getInstance().export2KML(sufix, naviptList, naviLineList, naviTrackList);
            _daochuInfo = String.format(getString(R.string.daochu_format), naviptList.size(), naviLineList.size(),
                    naviTrackList.size(), IECenter.EXDIR_KML + sufix) + ".KML";

            _savePath = IECenter.EXDIR_KML + sufix + ".KML";
        }

        if (style == 2) {
            //导出的SHP路径
            String strSHPRoot = Environment.getExternalStorageDirectory() + "/SHP/";
            if (!SysUtils.getInstance().is240x320) {
                strSHPRoot = Start.RUNDIR + "/HCMap_EX/SHP/";
            }

            final String strSHPDir = strSHPRoot + sufix + "/";
            File file2 = new File(strSHPDir);
            if (!file2.exists()) {
                file2.mkdirs();
            }
            String command = sufix + " -o " + strSHPDir + sufix + " " + _savePath;
            int result = JniUtils.getInstance().receiveCMD(command);
            if (result == 0) {
                _savePath = strSHPDir;
            } else {
                _savePath = "导出异常, ret=" + result;
            }
        }

    }

    String _savePath = "";
    //    /storage/sdcard0/BHCMapper/rundirVersion_1.0.1
    private void finishDaochu() {

        if (_needshare) {
            if (_savePath.length() > 1) {
                //分享文件
                ArrayList<Uri> files = new ArrayList<Uri>();
                files.add(Uri.fromFile(new File(_savePath)));
                Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);//发送多个文件
                intent.setType("*/*");//多个文件格式
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,files);//Intent.EXTRA_STREAM同于传输文件流
                startActivity(intent);

            }
            _needshare = false;
        } else {
            _daochuInfo = _daochuInfo.replace("/mnt/sdcard2", "SD卡");
            new AlertDialog(context, getString(R.string.daochu_cwd), _daochuInfo, false, 0, this).show();
        }
    }

    private final int DIALOG_SET_TOPBAR = 1;

    @Override
    public void onDialogItemClick(int requestCode, int position, String item) {
        if (position < 0) {
            position = 0;
        }
        switch (requestCode) {
            case DIALOG_SET_TOPBAR:
                onDoNext(position);
                break;
            default:
                break;
        }
    }

    private final int REQUEST_TO_TOP_MENU = 30;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            _order = 0;
                            initData();
                            break;
                        case 1:
                            _order = 1;
                            initData();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 0) {
            //导出成功
            setResult(RESULT_OK);
            finish();
        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, String>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.daochu_view, parent, false);
            }

            TextView tv_index = BaseViewHolder.get(convertView, R.id.tv_index);
            TextView tv_name = BaseViewHolder.get(convertView, R.id.tv_name);
            TextView tv_distance = BaseViewHolder.get(convertView, R.id.tv_distance);
            TextView tv_area = BaseViewHolder.get(convertView, R.id.tv_area);
            TextView tv_time = BaseViewHolder.get(convertView, R.id.tv_time);
            TextView iv_check = BaseViewHolder.get(convertView, R.id.iv_check);
            iv_check.setTypeface(AppContext.getInstances().getIconfont());

            final Entry<Integer, String> data = getItem(position);
            int index = data.getKey();

            tv_distance.setText("");
            tv_area.setText("");

            boolean isSelected = false;
            if (_mode == 0 && index >= 0 && index < mNaviptList.size()) {
                WayPoint navipt = mNaviptList.get(index);
                tv_index.setText(getString(R.string.navipt) + new DecimalFormat("00").format(index + 1));
                tv_name.setText(navipt.getName());
                tv_time.setText(SysUtils.getInstance().getTimeStr(navipt.getDatetime()));
                isSelected = hashMap.get(navipt.getId());
            } else if (_mode == 1 && index >= 0 && index < mNaviLineList.size()) {
                Route naviline = mNaviLineList.get(index);
                tv_index.setText(getString(R.string.naviline) + new DecimalFormat("00").format(index + 1));
                tv_name.setText(naviline.getName());
                tv_time.setText(SysUtils.getInstance().getTimeStr(naviline.getDatetime()));
                isSelected = hashMap1.get(naviline.getId());
            } else if (_mode == 2 && index >= 0 && index < mNaviTrackList.size()) {
                WayTrail waytrail = mNaviTrackList.get(index);
                tv_index.setText(getString(R.string.navitrack) + new DecimalFormat("00").format(index + 1));
                tv_name.setText(waytrail.getName());
                tv_time.setText(SysUtils.getInstance().getTimeStr(waytrail.getStarttime()));
                isSelected = hashMap2.get(waytrail.getId());
            }

            if (isSelected) {
                iv_check.setText(R.string.fonticon_xz);
            } else {
                iv_check.setText(R.string.fonticon_wxz);
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }


}
